package com.bcsq.todolistdemo.demo.adapter;

import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bcsq.todolistdemo.R;

/**
 * 列表适配器
 */
public class IconAdapter extends BaseAdapter {
    Context mContext;
    Pair<Integer, String>[] data ;

    public IconAdapter(){

    }

    public IconAdapter(Context mContext, Pair<Integer, String>[] data) {
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public int getCount() {
        if (data == null)
            return 0;

        return data.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder;
        if (view == null){
            viewHolder = new ViewHolder();
            LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            view= layoutInflater.inflate(R.layout.item_icon_choose, viewGroup, false);
            viewHolder.imageView = view.findViewById(R.id.iv_icon);
            viewHolder.textView = view.findViewById(R.id.tv_name);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.imageView.setImageResource(data[i].first);
        viewHolder.textView.setText(data[i].second);

        return view;
    }

    static class ViewHolder{
        ImageView imageView;
        TextView textView;
    }
}
